package com.sdhs.common.utils.mqtt.service.impl;


import cn.hutool.json.JSONUtil;

import com.sdhs.common.utils.TextUtil;
import com.sdhs.common.utils.mqtt.MqttMessageDTO;
import com.sdhs.common.utils.mqtt.constant.MqttMessageConstant;
import com.sdhs.common.utils.mqtt.service.MqttGateway;
import com.sdhs.common.utils.mqtt.service.MqttService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

/**
 * @author cqp1116
 * @date
 */
@Service
public class MqttServiceImpl implements MqttService {
    @Autowired(required = false)
    private MqttGateway mqttGateway;

    /**
     * 发送消息
     * @param messageDTO
     *
     */
    @Override
    public void sendMessage(MqttMessageDTO messageDTO) {
        String fromUserName = "system";
        String toObjects = messageDTO.getTo();
        messageDTO.setFrom(fromUserName);
        messageDTO.setSendTime(new Date());
        String dataJson = JSONUtil.toJsonStr(messageDTO);
        if (messageDTO.getToType().equals(MqttMessageConstant.ToTypes.ALL)) {
            mqttGateway.sendToMqtt(dataJson, MqttMessageConstant.TOPIC_USER_ALL_PATH);
        }
        if (messageDTO.getToType().equals(MqttMessageConstant.ToTypes.LONGIN)) {
            mqttGateway.sendToMqtt(dataJson, MqttMessageConstant.TOPIC_USER_PATH + fromUserName);
        }
        if (messageDTO.getToType().equals(MqttMessageConstant.ToTypes.APPLICATION)) {
            Arrays.stream(toObjects.split(",")).forEach(toApp -> {
                mqttGateway.sendToMqtt(dataJson, MqttMessageConstant.TOPIC_APP_PATH + toApp);
            });
        }
        if (messageDTO.getToType().equals(MqttMessageConstant.ToTypes.TOKEN)) {
            Arrays.stream(toObjects.split(",")).forEach(toToken -> {
                mqttGateway.sendToMqtt(dataJson, MqttMessageConstant.TOPIC_TOKEN_PATH+ toToken);
            });
        }
        if (messageDTO.getToType().equals(MqttMessageConstant.ToTypes.USER)) {
            Arrays.stream(toObjects.split(",")).forEach(toUser -> {
                mqttGateway.sendToMqtt(dataJson, MqttMessageConstant.TOPIC_USER_PATH + toUser);
            });
        }
    }

    @Override
    public void sendToUser(String toUserName, Object data) {
        this.sendToUser("9", toUserName, data);
    }

    /**
     * 给指定用户发送消息
     * @param msgType
     * @param toUserName, 支持逗号隔离开
     * @param data
     */
    @Override
    public void sendToUser(String msgType, String toUserName, Object data) {
        String fromUserName = "system";
        String[] arrToUser = toUserName.split(",");
        for (String toUser : arrToUser) {
            MqttMessageDTO messageDTO = new MqttMessageDTO(fromUserName, toUser, msgType, data);
            String dataJson = JSONUtil.toJsonStr(messageDTO);
            mqttGateway.sendToMqtt(dataJson, MqttMessageConstant.TOPIC_USER_PATH + toUser);
        }
    }

    /**
     * 给当前登录用户发消息
     * @param data
     */
    @Override
    public void sendToLoginUser(String fromUser, Object data) {
        if(TextUtil.isNull(fromUser)){
            fromUser ="system";
        }
        sendToUser(fromUser, data);
    }


    @Override
    public void sendAll(Object data) {
        this.sendAll("9", data);
    }

    /**
     * 给全部通信信道发送信息
     * @param msgType
     * @param data
     */
    @Override
    public void sendAll(String msgType, Object data) {
        String fromUserName = "system";
        MqttMessageDTO messageDTO = new MqttMessageDTO(data);
        messageDTO.setMsgType(msgType);
        messageDTO.setFrom(fromUserName);
        String dataJson = JSONUtil.toJsonStr(messageDTO);
        mqttGateway.sendToMqtt(dataJson, MqttMessageConstant.TOPIC_USER_ALL_PATH);
    }

    /**
     * 给某个应用发送信息
     * @param appId
     * @param data
     */
    @Override
    public void sendApplication(String appId, Object data) {
        this.sendApplication("9", appId, data);
    }

    /**
     * 给某个应用发送信息
     * @param msgType
     * @param appId
     * @param data
     */
    @Override
    public void sendApplication(String msgType, String appId, Object data) {
        String fromUserName = "system";
        MqttMessageDTO messageDTO = new MqttMessageDTO(fromUserName, appId, data);
        messageDTO.setMsgType(msgType);
        String dataJson = JSONUtil.toJsonStr(messageDTO);
        mqttGateway.sendToMqtt(dataJson, MqttMessageConstant.TOPIC_APP_PATH + appId);
    }

    /**
     * 给某个应用发送信息
     * @param topic
     * @param data
     */
    @Override
    public void sendTopic(String topic, Object data) {
        this.sendTopic("9", topic, data);
    }

    /**
     * 给某个应用发送信息
     * @param msgType
     * @param topic
     * @param data
     */
    @Override
    public void sendTopic(String msgType, String topic, Object data) {
        String fromUserName = "system";
        MqttMessageDTO messageDTO = new MqttMessageDTO(fromUserName, topic, data);
        messageDTO.setMsgType(msgType);
        String dataJson = JSONUtil.toJsonStr(messageDTO);
        mqttGateway.sendToMqtt(dataJson, topic);
    }

    /**
     * 公共主题信息, 无需登录即可接收
     * @param  msgType
     * @param topic
     * @param data
     */
    @Override
    public void sendPubicTopic(String msgType, String topic, Object data) {
        String  fromUserName = "system";
        MqttMessageDTO messageDTO = new MqttMessageDTO(fromUserName, MqttMessageConstant.TOPIC_PUBLIC_PATH + topic, data);
        messageDTO.setMsgType(msgType);
        String dataJson = JSONUtil.toJsonStr(messageDTO);
        mqttGateway.sendToMqtt(dataJson, MqttMessageConstant.TOPIC_PUBLIC_PATH + topic);
    }

    /**
     * 更新
     */
    @Override
    public void updateOnline() {

    }
}
